package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.ModifyName;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.*;
import com.rightsidetech.system.dtos.response.CompanyAreaResp;
import com.rightsidetech.system.dtos.response.CompanyManageAreaResp;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysCompanyAreaService;
import com.rightsidetech.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2021-10-22 11:39
 */
@RestController
@RequestMapping("/admin/sys_Area")
@ShenyuSpringMvcClient(path = "/admin/sys_Area/**")
@Api(tags = "admin:大区相关")
public class SysAreaController extends BaseController {
    @Resource
    private SysCompanyAreaService sysCompanyAreaService;
    @Resource
    private SysUserService sysUserService;

    @Log
    @Token
    @ApiOperation("大区下拉框加载数据")
    @GetMapping("/search")
    public ResponseData<List<CompanyManageAreaResp>> selectList() throws Exception {
        Long loginId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = null;
        if (loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }
        return sysCompanyAreaService.selectList(areaId);
    }

    @Log
    @Token
    @ApiOperation("大区下拉框加载数据--（只查询开启状态大区）")
    @GetMapping("/search_open")
    public ResponseData<List<CompanyManageAreaResp>> searchOpen() throws Exception {
        Long loginId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        Long areaId = null;
        if (loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }
        return sysCompanyAreaService.searchOpen(areaId);
    }

    @Log
    @Token
    @ApiOperation("大区列表")
    @GetMapping("/page")
    public ResponseData<PageInfo<CompanyAreaResp>> page(CompanyAreaReq req) throws Exception {
        return sysCompanyAreaService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add")
    @ApiOperation("新增大区")
    @ModifyLog(serviceClass = SysCompanyAreaService.class,modifyType = ModifyName.SAVE)
    public ResponseData saveCompanyArea(@RequestBody CompanyAreaAddReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysCompanyAreaService.saveCompanyArea(req, loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改大区")
    @ModifyLog(serviceClass = SysCompanyAreaService.class)
    public ResponseData updateCompanyArea(@RequestBody CompanyAreaUpdateReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysCompanyAreaService.updateCompanyArea(req, loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除大区")
    @ModifyLog(serviceClass = SysCompanyAreaService.class)
    public ResponseData updateCompanyArea(@RequestBody DelReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysCompanyAreaService.delCompanyArea(req, loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/updateStatus")
    @ApiOperation("修改大区状态-开启/关闭")
    @ModifyLog(serviceClass = SysCompanyAreaService.class)
    public ResponseData updateStatus(@RequestBody UpdateStatusReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysCompanyAreaService.updateStatus(req, loginInfoId);
    }


}
